package com.aixin.csopservice.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.BigExcelWriter;

import java.io.File;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ExcelHelper {

    private static final Object[] emptyArg = new Object[]{};

    public static <T> File writeXlsx(String[] keys, String[] columnNames, List<T> dataList, Boolean hidePrivate) throws Exception {
        List<?> headerRow = CollUtil.newArrayList(columnNames);
        List<List<?>> rows = new ArrayList<>();
        rows.add(headerRow);
        if (dataList.size() > 0) {
            Method[] methods = new Method[keys.length];
            T entity = dataList.get(0);
            for (int i = 0; i < keys.length; i++) {
                methods[i] = entity.getClass().getMethod("get" + keys[i].substring(0, 1).toUpperCase() + keys[i].substring(1));
            }

            for (int i = 0; i < dataList.size(); i++) {
                rows.add(entityToList(dataList.get(i), methods, keys, hidePrivate));
            }
        }
        File excelFile = new File(System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + ".xlsx");
        BigExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getBigWriter(excelFile);
        writer.write(rows);
        writer.close();
        return excelFile;
    }

    private static <T> List<?> entityToList(T entity, Method[] methods, String[] keys, Boolean hidePrivate) {
        try {
            Object[] values = new Object[methods.length];
            for (int i = 0; i < methods.length; i++) {
                Object ret = methods[i].invoke(entity, emptyArg);
                if (ret == null) {
                    values[i] = null;
                } else if (keys[i].equals("gender")) {
                    values[i] = "0".equals(ret.toString()) ? "男" : "女";
                } else if (keys[i].equals("lastUseTime") || keys[i].equals("dueTime") || keys[i].equals("firstEffTime")) {
                    values[i] = ret.toString().substring(0, 10);
                } else if (keys[i].equals("custStatus")) {
                    values[i] = "0".equals(ret.toString()) ? "正常" : "异常";
                } else if (hidePrivate && keys[i].equals("phoneNumber")) {
                    // 数据脱敏
                    String phoneNumber = (String) ret;
                    values[i] = phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7);
                } else if (keys[i].equals("lastUseDate")) {
                    // LocalDate
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    values[i] = ((LocalDate) ret).format(formatter);
                } else {
                    values[i] = ret;
                }
            }
            return CollUtil.newArrayList(values);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
